package com.A1908.dao.impl;

import com.A1908.dao.UserDao;
import com.A1908.entity.MyAdmin;
import com.A1908.entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl extends BaseDao implements UserDao {
    Connection connection = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    @Override
    public User findUser(User user) {
        connection = super.conn();
        String sql = "";
        Object [] objs=null;
        if (user.getUserEmail()==null){
            sql = "SELECT * FROM `user` WHERE phoneNumber=? AND `password`=?;";
            objs= new Object[]{user.getPhoneNumber(), user.getPassword()};
        }else {
            sql="SELECT * FROM `user` WHERE phoneNumber=? OR userEmail=?;";
            objs= new Object[]{user.getPhoneNumber(), user.getUserEmail()};
        }
        pstmt=pstmt(connection,sql,objs);
        try {
            rs=pstmt.executeQuery();
            User u=null;
            while (rs.next()){
                u=new User();
                u.setId(rs.getInt(1));
                u.setUsername(rs.getString(2));
                u.setPassword(rs.getString(3));
                u.setCreatDate(rs.getString(4));
                u.setPhoneNumber(rs.getString(5));
                u.setWebIsOnline(rs.getInt(6));
                u.setAndroidIsOnline(rs.getInt(7));
                u.setUserImg(rs.getString(8));
                u.setUserEmail(rs.getString(11));
            }
            return u;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cloesAll(connection,pstmt,rs);
        }
        return null;
    }

    public boolean userVerify(User user){
        connection = super.conn();
        String sql="SELECT * FROM `user` WHERE  phoneNumber=? or userEmail=?;";
        Object[]objs={user.getPhoneNumber(),user.getUserEmail()};
        pstmt=pstmt(connection,sql,objs);
        try {
            rs=pstmt.executeQuery();
            if (rs.next()){
                return false;
            }
            else {
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    @Override
    public MyAdmin findAdmin(MyAdmin myAdmin) {
        connection = super.conn();
        String sql = "SELECT * FROM admin WHERE adminUserName=? AND adminPassword=?;";
        Object [] objs={myAdmin.getAdminUserName(),myAdmin.getAdminPassword()};
        pstmt=pstmt(connection,sql,objs);
        try {
            rs=pstmt.executeQuery();
            MyAdmin ma=null;
            while (rs.next()){
                ma=new MyAdmin();
                ma.setId(rs.getInt(1));
                ma.setAdminUserName(rs.getString(2));
                ma.setAdminCreatDate(rs.getString(4));
                ma.setAdminLevel(rs.getInt(5));
                ma.setAdminPhoneNum(rs.getString(6));
                ma.setAdminAddress(rs.getString(7));
            }
            return ma;
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            cloesAll(connection,pstmt,rs);
        }
        return null;
    }

    @Override
    public User getUserById(int id) {
        connection = conn();
        String sql = "SELECT * FROM `user` WHERE `user`.id = ?";
        Object[] objects = {id};
        User user = null;
        pstmt = pstmt(connection,sql,objects);

        try {
            rs = pstmt.executeQuery();
            if (rs.next()){
                user = new User();
                user.setId(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
                user.setCreatDate(rs.getString(4));
                user.setPhoneNumber(rs.getString(5));
                user.setWebIsOnline(rs.getInt(6));
                user.setAndroidIsOnline(rs.getInt(7));
                user.setUserImg(rs.getString(8));
                user.setUserEmail(rs.getString(11));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return user;
    }

    @Override
    public int addUser(User user) {
        String sql = "INSERT INTO `user` VALUES (DEFAULT,?,?, NOW(), ?, 0, 0, NULL, NULL, NULL,?)";
        Object[] objects = {
                user.getUsername(),
                user.getPassword(),
                user.getPhoneNumber(),
                user.getUserEmail()
        };
        return executeUpdate(sql,objects);
    }

    @Override
    public int editUser(User user) {
        String sql = "update user set username = ?,password = ?,creatDate = ?,phoneNumber = ?,userImg = ?,userEmail = ?\n" +
                "where id = ?;";
        Object[] objects = {
                user.getUsername(),
                user.getPassword(),
                user.getCreatDate(),
                user.getPhoneNumber(),
                user.getUserImg(),
                user.getUserEmail(),
                user.getId()
        };
        return executeUpdate(sql,objects);
    }

    @Override
    public int deleteUser(User user) {
        return 0;
    }

}
